<?php

namespace App\Http\Controllers\DingTalk;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\User as DingTalkUser;
use App\Models\DingTalkDept;

class DingTalkUserController extends Controller
{

    //DingTalkUser
    public function index()
    {
        if (post('?')) {
            $_category = post("_category");
            $usermodel = DingTalkUser::relatedJoin();
            if ((int)$_category > 1)
                $usermodel->where(["dept_id" => $_category]);
            return $usermodel->getGrid();
        } else {
            $param = F();
            $param['categroy'] = $this->CategoryAll();
            return view('DingTalk.DingTalkUserController.index',  $param);
        }
    }

    public function add()
    {
        // dd(Menu::treeGrid()->toArray());
        if (post("?")) {
            $data = post("data");
            try {
                begin();
                $gdata = DingTalkUser::create(unsetNull($data));
                $gdata->saveitems(json_decode(post('items'), true));
                commit();
            } catch (\Exception $e) {
                rollback();
                return error($e->getMessage());
            }

            return success("保存成功！");
        } else {
            $param = F('edit');
            return view('BaseData.UserController.edit',  $param);
        }
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit()
    {
        $id = get("id");
        $gdata = DingTalkUser::where("id", $id)->first();
        // dd(Menu::treeGrid()->toArray());
        if (post("?")) {
            $data = post("data");
            try {
                begin();
                $gdata->update(unsetNull($data));
                $gdata->saveitems(json_decode(post('items'), true));
                commit();
            } catch (\Exception $e) {
                rollback();
                return error($e->getMessage());
            }

            return success("保存成功！");
        } else {
            $param = F('edit');
            $param['gdata'] = $gdata;
            $param['items'] = $gdata->rolesList()->relatedJoin()->get();
            return view('BaseData.UserController.edit',  $param);
        }
    }

    public function delete()
    {
        begin();
        $ids = request()->post('ids');;
        // Category::whereIn("id", $ids)->delete();
        foreach (DingTalkUser::whereIn("id", $ids)->get() as  $k => $v) {
            //盘点能否删除
            $v->delete();
        }
        commit();
        return success("删除成功！");
    }


    public function Category()
    {
        if (request()->isMethod('post')) {
            return (DingTalkDept::select("dept_id as id", "name", "parent_id")->treeGrid());
        } else {
            return view('common.category.category',   F('Category', 'category', 'common'));
        }
    }
    public function CategoryAll()
    {
        return  DingTalkDept::select("name", "dept_id as id", "parent_id as pId")->get();
    }

    public function addCategory()
    {
        if (request()->isMethod('post')) {
            $data = request()->post('data');

            try {
                begin();

                DingTalkDept::create(array_filter($data));
                // $gdata->save();
                commit();
            } catch (\Exception $e) {
                rollback();
                return error($e->getMessage());
            }

            return success("保存成功！");
        } else {
            // $param = F();
            $param['selectdata']['data'] = DingTalkDept::select("dept_id as id", "name")->get();
            $param['selectdata']['name'] = "data[parent_id]";
            return view('common.category.addCategory',  $param);
        }
    }
    public function editCategory()
    {
        $id = request()->get('id');
        if ($id == 1)
            throwError("主类不能编辑！");
        $gdata = DingTalkDept::where(['dept_id' => $id])->first();

        if (request()->isMethod('post')) {
            $data = request()->post('data');
            try {
                begin();
                $gdata->update(unsetNull($data));
                commit();
            } catch (\Exception $e) {
                rollback();
                return error($e->getMessage());
            }

            return success("保存成功！");
        } else {
            // $param = F();

            $param['selectdata']['data'] = DingTalkDept::select("dept_id as id", "name")->get();

            $param['selectdata']['name'] = "data[parent_id]";
            $param['selectdata']['default'] = $gdata->parent_id;
            $param['gdata'] = $gdata;

            return view('common.category.addCategory',  $param);
        }
    }
    public function deleteCategory()
    {
        begin();
        $ids = request()->post('ids');;
        // Category::whereIn("id", $ids)->delete();
        foreach (DingTalkDept::whereIn("dept_id", $ids)->get() as  $k => $v) {
            $v->delete();
        }
        commit();
        return success("删除成功！");
    }
}
